package com.softeem.oss_music.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.oss_music.comm.R;
import com.softeem.oss_music.entity.User;
import com.softeem.oss_music.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "用户管理")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Operation(summary = "用户列表", description = "根据提供的页码和数据条数实现分页查询")
    @Parameters({
            @Parameter(name = "page", description = "页码"),
            @Parameter(name = "limit", description = "数据条数")
    })
    @GetMapping("/user/list")
    public R list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer limit){
        IPage<User> ipage = new Page<>(page, limit);
        List<User> list = userService.list(ipage);
        //查询总记录数
        long count = userService.count();
        return R.success().setData(list).setCount(count);
    }
    
    @Operation(summary = "禁用用户", description = "将用户拉入黑名单")
    @Parameters({
            @Parameter(name = "id", description = "用户ID")
    })
    @PostMapping("/user/disable")
    public R disable(Integer id){
        if (id == null) {
            return R.error().setMessage("用户ID不能为空");
        }
        User user = userService.getById(id);
        if (user == null) {
            return R.error().setMessage("用户不存在");
        }
        // 将用户状态设置为禁用（0）
        user.setStatus(0);
        boolean result = userService.updateById(user);
        return result ? R.success().setMessage("用户已被禁用") : R.error().setMessage("禁用失败");
    }
    
    @Operation(summary = "启用用户", description = "将用户移出黑名单")
    @Parameters({
            @Parameter(name = "id", description = "用户ID")
    })
    @PostMapping("/user/enable")
    public R enable(Integer id){
        if (id == null) {
            return R.error().setMessage("用户ID不能为空");
        }
        User user = userService.getById(id);
        if (user == null) {
            return R.error().setMessage("用户不存在");
        }
        // 将用户状态设置为正常（1）
        user.setStatus(1);
        boolean result = userService.updateById(user);
        return result ? R.success().setMessage("用户已被启用") : R.error().setMessage("启用失败");
    }
    @Operation(summary = "用户登录", description = "根据提供的用户名和密码实现登录")
    @Parameters({
            @Parameter(name = "username", description = "用户名"),
            @Parameter(name = "password", description = "密码")
    })
    @GetMapping("/user/login")
    public R login(String username, String password){
        //todo 登录
        return userService.login(username,password);
    }
   @Operation(summary = "用户注册", description = "根据提供的用户名和密码实现注册")
   @Parameters({
           @Parameter(name = "username", description = "用户名"),
           @Parameter(name = "password", description = "密码")
   })
    @PostMapping("/user/register")
    public R register(User user){
        //todo 注册
        return userService.register(user);
    }

}
